package com.uphone.kingmall.activity;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.uphone.kingmall.adapter.XianShiGouListAdpater;
import com.uphone.kingmall.adapter.XianShiTabAdapter;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.bean.FlashSalesListBean;
import com.uphone.kingmall.listener.OnItemClickListener3;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.ConstansUtils;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.RecycleViewDivider;
import com.uphone.kingmall.utils.ToastUtils;
import com.uphone.kingmall.utils.intent.IntentUtils;

import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : SuMeng
 * e-mail : 986335838@qq.com
 * time   : 2019/02/26
 * desc   : 限时购列表页
 */
public class XianShiGouListActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_xianshigou_list)
    RecyclerView rvXianShiGouList;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.rv_tab)
    RecyclerView rvTab;
    @BindView(R.id.tv_empty)
    TextView tvEmpty;
    private ArrayList<FlashSalesListBean.DataBean> flashSalesList;
    private XianShiGouListAdpater xianShiGouListAdpater;
    private int page;
    private int clock;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_xianshigou;
    }

    @Override
    public void initView() {
        setStatusBar();
    }

    @Override
    public void initData() {
        xianShiGouListAdpater = new XianShiGouListAdpater();
        rvXianShiGouList.setAdapter(xianShiGouListAdpater);
        rvXianShiGouList.setLayoutManager(new LinearLayoutManager(XianShiGouListActivity.this));
        rvXianShiGouList.addItemDecoration(new RecycleViewDivider(
                XianShiGouListActivity.this, LinearLayout.VERTICAL, 5, getResources().getColor(R.color.colorTransparent)));


        //创建默认的线性LayoutManager
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        rvTab.setLayoutManager(mLayoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        rvTab.setHasFixedSize(true);
        rvTab.setNestedScrollingEnabled(false);
        XianShiTabAdapter xianShiTabAdapter = new XianShiTabAdapter(rvTab, mLayoutManager);
        rvTab.setAdapter(xianShiTabAdapter);
        ArrayList<Object> list = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            list.add(new Object());
        }
        xianShiTabAdapter.setNewData(list);

        xianShiTabAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        xianShiTabAdapter.setOnItemClickListener(new OnItemClickListener3() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View v, int position) {
                loadData(position, 1);
            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                loadData(clock, 1);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                loadData(clock, ++page);
            }
        });
        xianShiGouListAdpater.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                IntentUtils.getInstance().with(XianShiGouListActivity.this, GoodsDetailActivity.class)
                        .putInt(ConstansUtils.ID, flashSalesList.get(position).getGoodsId())
                        .putInt(ConstansUtils.TYPE, GoodsDetailActivity.TYPE_FLASH).start();
            }
        });

        Date myDate = new Date();
        int hours = myDate.getHours();
        //初始化数据
        xianShiTabAdapter.setItemClick(hours);
        loadData(hours, 1);

    }

    private void loadData(int clock, int page) {
        this.clock = clock;
        this.page = page;
        HttpParams params = new HttpParams();
        params.put("isIndex", "0");
        params.put("clock", clock);
        params.put("page", page + "");
        params.put("limit", 10);

        OkGoUtils.normalRequest(MyUrl.getFlashSales, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                LogUtils.e("限时抢：" + body);
                final FlashSalesListBean catsListBean = GsonUtils.getGson().fromJson(body, FlashSalesListBean.class);
                if (catsListBean.getCode() == 0) {
                    if (flashSalesList == null) {
                        flashSalesList = new ArrayList<>();
                    }
                    if (XianShiGouListActivity.this.page == 1) {
                        flashSalesList.clear();
                    }
                    if (catsListBean.getData() != null && catsListBean.getData().size() > 0) {
                        flashSalesList.addAll(catsListBean.getData());
                    } else if (XianShiGouListActivity.this.page != 1) {
                        ToastUtils.showShortToast(XianShiGouListActivity.this, "暂无更多数据！");
                    }
                    xianShiGouListAdpater.setNewData(flashSalesList);
                }

                if (flashSalesList == null || flashSalesList.size() == 0) {
                    tvEmpty.setVisibility(View.VISIBLE);
                } else {
                    tvEmpty.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(Response<String> response) {
            }
        });
    }

    @OnClick(R.id.tv_title)
    public void onViewClicked() {
        onBackPressed();
    }


}
